package com.shuaitao.newsystem.config;


import com.shuaitao.newsystem.filter.JwtAuthenticationFilter;
import com.shuaitao.newsystem.handler.CustomAuthenticationEntryPoint;
import com.shuaitao.newsystem.handler.MyAccessDeniedHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.sql.DataSource;

@Configuration
//@EnableGlobalMethodSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private DataSource dataSource;
        @Autowired
    private MyAccessDeniedHandler accessDeniedHandler;
    @Autowired
    private CustomAuthenticationEntryPoint authenticationEntryPoint;
    @Autowired
    private UserDetailsService userDetailsService;

    @Bean
    public PersistentTokenRepository persistentTokenRepository() {
        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        jdbcTokenRepository.setDataSource(dataSource);
//        jdbcTokenRepository.setCreateTableOnStartup(true);
        return jdbcTokenRepository;
    }

        @Autowired
    private JwtAuthenticationFilter jwtAuthenticationFilter;
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                .antMatchers("/login/**").anonymous()
                .antMatchers("/").anonymous()
                .antMatchers("/static/**").anonymous()
                .antMatchers("/news/get*AllNews").anonymous()
                .antMatchers("/news/getDetailNews/**").anonymous()
                .antMatchers("/comment/commentInit").anonymous()
                .antMatchers("/comment/getAdminDetailComment").anonymous()
                .antMatchers("/comment/getDetailComment").anonymous()
                .antMatchers("/admin/toAdmin").anonymous()
                .antMatchers("/topic/toTopicEditDetail").anonymous()
                .antMatchers("/topic/toTopicAdd").anonymous()
                .antMatchers("/topic/getAllTopic").anonymous()
                .antMatchers("/topic/toTopicEdit").anonymous()
                .antMatchers("/news/toAddNews").anonymous()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .anyRequest().authenticated()
//                    .and().rememberMe().tokenRepository(persistentTokenRepository())
//                    .tokenValiditySeconds(600).userDetailsService(userDetailsService)
                .and().csrf().disable();  //关闭csrf防护
        http.cors();
        http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
        http.exceptionHandling().accessDeniedHandler(accessDeniedHandler);
        http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
//        String password = passwordEncoder.encode("123456");
//        auth.inMemoryAuthentication().withUser("lucy").password(password).roles("admin").authorities("aaaa");
        auth.userDetailsService(userDetailsService).passwordEncoder(password());

    }


    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Bean
    PasswordEncoder password() {
        return new BCryptPasswordEncoder();
    }
}
